﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.core.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.widget.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.mouse.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.position.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.draggable.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.dialog.js" />
///<reference path="../../scripts/jQueryUI/jquery.ui.datepicker.js" />
///<reference path="../../scripts/jquery.query.js" />
///<reference path="../../scripts/yardiTip/yardiTip.js" />
///<reference path="../../scripts/kooboo/kooboo.mvcvalidation.js" />





$(function () {


    $("#pagesize").change(function () {
        var pagesize = $(this).val();

        var url = $.query.set('pagesize', pagesize).set('page', 1);

        window.location.href = url;
    });

    // Dropdown-Button
    $('.dropdown-button span').click(function () {
        var ul = $(this).siblings('ul');
        if (ul.hasClass('hide')) {
            ul.slideDown('fast');
            ul.removeClass('hide');
            $('.dropdown-button').addClass('active');
        } else {
            ul.slideUp('fast');
            ul.addClass('hide');
            $('.dropdown-button').removeClass('active');
        }
    });

    $('body').click(function (evt) {
        if (!$(evt.target).is('.dropdown-button span')) {
            $('.dropdown-button ul').slideUp('fast');
            $('.dropdown-button ul').addClass('hide');
            $('.dropdown-button').removeClass('active');
        }
    });

    $(".tooltip-link").yardiTip();

    //Tabs
    $('.tabs li').click(function () {
        switchTab($(this));
        return false;
    })


    //Dialog
    $(".dialog").each(function (index, item) {
        var height = $(item).css("height");
        var width = $(item).css("width");

        if (height.indexOf("px") > -1) {
            height = parseFloat(height.substr(0, height.length - 2));
        }

        if (width.indexOf("px") > -1) {
            width = parseFloat(width.substr(0, width.length - 2));
        }


        var height = height || 500; //return 500 when the height is undefined or zero 
        var width = width || 1000;

        $(item).dialog({
            autoOpen: false,
            height: height,
            //resize: 'auto',
            width: width,
            modal: true
        })
    });
    //Dialog link
    $('.dialog-link').live("click", function (e) {
        target = $(this).attr("href");
        e.preventDefault();
        $(target).dialog("open");
        return false;
    });

});




$.fn.mvcvalidate.defaults.complete = function (isValid, form) {
    if (isValid === false) {

        var firstValid = $(".field-validation-error:first", form);

        var tabContent = firstValid.parents(".tab-content:first", form);
        if (tabContent) {
            var id = tabContent.attr("id");

            var tab = $(".tabs").find("a[href=#" + id + "]").parents("li:first");
            if (tab.size() > 0) {
                switchTab(tab);
            }
        }

        $(firstValid.attr("selector"), form).focus();
    }
}


function switchTab(tab) {
    tab.siblings().removeClass("current");
    tab.addClass("current");
    tab.parents('.tabs').siblings('.tab-content').addClass("hide");
    $(tab.children().attr('href')).removeClass("hide");
}